package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122613738";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCB6ZeKf1pDSUiEiXqM9hrkyIC2LbwVKj5NAbb+8b98ylF8nfagw/WmeWw5XUmoLhDxk2ZiJ+GmyD4Z0Ufemf2XDbIUABJntw8d3upNdRtUfno0lHmB8134cem8adcqpMAYAvLVMLgnEFRDtBd1eQdugy+sdQoIyS6qRll/NFpXvxluY6wVdX07fBGQ8mu3dpQee3K+3MJdgcV3KeSQoJabsN1tGPncZ1aArMOIEE/ys1NuLxwcHnW3ximTin6lmwfFjb7wnRQkFO+QGbPFWear5SWIz3/YCi8RZaVhxczv1TWz78mZGCvx08+GshDDwmbUGeJ+sF0emhBVKRhlsv/tAgMBAAECggEAVhon4hc+D9KGSbGEk+CZOoDZLeVp/u0clZVJX/eB3oeJMnrPdwXpzggz/mEh7GF7wWDcProICK422o2mgJa3SGI/YJobmLnvPvD7gZLYJkwdWrP9RG+0HHIDZGkRvqWq2z85CqtboihkcOrvkWBB2LEB6982Y1RTpW8y6egc6II96O1+D3PbeFRPJJ5Nel+jIMUMW30N0UjMErfcHpli71kGNPd33kXUfoPsEO5J7ODl0LBC6YDW8SmTZFgNGZLowRIMenA6ZXKCfQ60IHvXQNhgaIzZK7WFHbhytU7y0yLkP4xe7C9guUFpa+64gPtUM7g4kNaMLn+g9svTpOzVUQKBgQDa1AtIMPIvaYtpNW2ZqWINK/9w6Ye6lHYfgs4wxr54aJan2PxqVyqkdIIVlEiJQnngoNFJixr1PDUgced7wjRHioB1dDi6dRBPul8hxhOnKTZnSa7bJwCxMpR7ia6zhiSufF46Vj0OJZoWU2ytr9EkyjXvEItnDV4PbjHoTrkwCwKBgQCX+vVzxswWCm1HGwJkSUUtShZ2nsuIp9YsH0nc6Y8gDsYPrpOJzhjeb+La55QJMrfvgRDaT+y/3SPbYn6gbguewSEmzejuRDfmcb2UtovFpc+3kcYU+b/055PjxOP26RQKogF0hiALSaevRG7IMUVFoKxP+qf2A5Xr4faJdTOy5wKBgQCoW/gKJM5QOA3wdhDK5ozvwWgYgTPSEyMGf2cQE/i7PwyaOtGJJ2NBf/oNjDZ6lFCcyF39DS9Cen4NZlVI3FPIrwDCXRbALgzMqoFCxNieBZE7ym7jYZCgxF+E5JsquAxEyp7clzkGR1vTzX+R6fEpMwJn2fzG3nv9tzRinR7RnQKBgGLEJosM4EhaklWpSc6ehyQ9h4ei/qZaXCbkVn4IEolE0JYUX7EojKTRlV2pLV0k5r/w4mDdm/+72LeTtLLRkR+kZsTu/7cIAkPyGr/wOblCXlDvnQi4nXSsokFdDMDDC9E1Izj0iRKM1TpjgJL84gWzuwdm34xTHDTk7fC6h9R1AoGARljqCjQyK6yFi2wI4p2080Lg6UBlxXKrGhVqSXlgrJUs8oa6weYbkkaj4NhkdZYQ7Xx2oYU0muZVKiYq8jkSxSW3yoVLw9YrYW3SJigsOju/fkX8a9E6+IMhW6C0PRIXmB6ClITbq0kDOaktmUoPFam5GAZM3W4Ep4BzdExrJvs=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgHP6h2w0WjFunM9RFyCNNBO6JnfRQQd4Kl+JhSoHEKBBjDh418kZB5jp3GV8EohxSUzUDjQxyhbmmFRyFy0H5JxZRLm5jBUHA5r9gkLrfqzy8/JVnO7hVLWr7g/EJ+yPvySSdgQZuV7INnKr12QkBTWvbjQVE4ZqFIS5RIzSIl/CGSsmsD6eIAzEJqc+XpojtvGVHbGcjYDVL9zcEiM4hThsnL2yaVSrsMQQ8FOUfDh4Z+se/oIZ6cJP1nAf7Bpuk70L+uhkmNGNFa3/Ao6snQp0c4yFoQ3+qTvcr/b/4mnG4qABNP9NpoDJtZjAocwyMNWM7v2fS6cF3JabRwWxKwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://m97jxt.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //支付超时时间
    private String timeout_express="1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
